package cn.tx.erp.sys.service.impl;

import cn.tx.erp.sys.domain.User;
import cn.tx.erp.sys.mapper.RoleMapper;
import cn.tx.erp.sys.mapper.UserMapper;
import cn.tx.erp.sys.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/7/11 18:30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public boolean save(User entity) {
        return super.save(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        // 根据用户id删除 用户角色 中间表数据
        roleMapper.deleteRoleUserByUid(id);
        // 删除用户头【如果是默认头像不删除，否则删除】
        return super.removeById(id);
    }

    @Override
    public boolean updateById(User entity) {
        return super.updateById(entity);
    }

    @Override
    public User getById(Serializable id) {
        return super.getById(id);
    }


    @Override
    public void saveUserRole(Integer uid, Integer[] ids) {
        this.roleMapper.deleteRoleUserByUid(uid);

        if (null != ids && ids.length > 0) {
            for (Integer rid : ids) {
                this.roleMapper.saveRoleUser(uid, rid);
            }
        }
    }
}
